﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using System.Data.Common;
using Watchdog.IFacade;
using Watchdog.Facade;

namespace Watchdog.Wcf {


    public partial class WatchdogDataService : IWatchdogDataService {

        #region IWatchdogDataService Members


        public List<TestUnit> TestUnitGetAll () {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestUnit_Get_List");
            return DataSetToTestUnitList (db.ExecuteDataSet (command));
        }

        public List<Schedule> ScheduleGetLocked () {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("Schedule_GetLocked");
            return DataSetToScheduleList (db.ExecuteDataSet (command));
        }

        public List<TestUnit> TestUnitGetByZone (string zone) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestUnit_GetByZone");
            db.AddInParameter (command, "Zone", DbType.String, zone);
            return DataSetToTestUnitList (db.ExecuteDataSet (command));
        }

        public TestUnit TestUnitGetByName ( string name ) {
            Database db = DatabaseFactory.CreateDatabase ("WatchdogConnectionString");
            DbCommand command = db.GetStoredProcCommand ("TestUnit_GetByName");
            db.AddInParameter (command, "Name", DbType.String, name);
            DataSet dataset = db.ExecuteDataSet (command);
            if (dataset.Tables["Table"].Rows.Count == 1)
                return DataRowToTestUnit (dataset.Tables["Table"].Rows[0]);
            else
                return new TestUnit ();
        }

        #endregion
    }
}

